package com.healthdata.service.impl;

import com.healthdata.entity.HospitalBedSize;
import com.healthdata.dao.HospitalBedSizeRepository;
import com.healthdata.service.HospitalBedSizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class HospitalBedSizeServiceImpl implements HospitalBedSizeService {

    @Autowired
    private HospitalBedSizeRepository hospitalBedSizeRepository;

    @Override
    public Map<String, Object> getHospitalBedDistribution() {
        // 查询需要的医院类型数据
        List<String> hospitalTypes = Arrays.asList("公立医院", "民营医院", "综合医院", "中医医院", "中西医结合医院", "民族医医院");
        List<HospitalBedSize> hospitalBeds = hospitalBedSizeRepository.findByNameIn(hospitalTypes);

        // 按照前端需要的顺序排序
        Map<String, HospitalBedSize> hospitalMap = hospitalBeds.stream()
                .collect(Collectors.toMap(HospitalBedSize::getName, item -> item));

        // 构建返回数据
        Map<String, Object> result = new HashMap<>();

        // 医院类型顺序
        List<String> categories = Arrays.asList("公立", "民营", "综合", "中医", "中西医", "民族");

        // 床位范围顺序
        List<String> bedRanges = Arrays.asList("400~499", "300~399", "200~299", "100~199", "50~99", "0~49");

        // 构建series数据
        List<Map<String, Object>> seriesList = new ArrayList<>();

        // 400~499
        seriesList.add(createSeriesData("400~499", categories, hospitalMap,
                h -> h.getBeds400To499() != null ? h.getBeds400To499() : 0));

        // 300~399
        seriesList.add(createSeriesData("300~399", categories, hospitalMap,
                h -> h.getBeds300To399() != null ? h.getBeds300To399() : 0));

        // 200~299
        seriesList.add(createSeriesData("200~299", categories, hospitalMap,
                h -> h.getBeds200To299() != null ? h.getBeds200To299() : 0));

        // 100~199
        seriesList.add(createSeriesData("100~199", categories, hospitalMap,
                h -> h.getBeds100To199() != null ? h.getBeds100To199() : 0));

        // 50~99
        seriesList.add(createSeriesData("50~99", categories, hospitalMap,
                h -> h.getBeds50To99() != null ? h.getBeds50To99() : 0));

        // 0~49
        seriesList.add(createSeriesData("0~49", categories, hospitalMap,
                h -> h.getBeds0To49() != null ? h.getBeds0To49() : 0));

        result.put("categories", categories);
        result.put("bedRanges", bedRanges);
        result.put("series", seriesList);

        return result;
    }

    private Map<String, Object> createSeriesData(String name, List<String> categories,
                                                 Map<String, HospitalBedSize> hospitalMap, java.util.function.Function<HospitalBedSize, Integer> extractor) {
        Map<String, Object> series = new HashMap<>();
        series.put("name", name);
        series.put("type", "bar");
        series.put("stack", "total");

        // 根据医院类型顺序获取数据
        List<Integer> data = new ArrayList<>();
        data.add(extractor.apply(hospitalMap.get("公立医院")));
        data.add(extractor.apply(hospitalMap.get("民营医院")));
        data.add(extractor.apply(hospitalMap.get("综合医院")));
        data.add(extractor.apply(hospitalMap.get("中医医院")));
        data.add(extractor.apply(hospitalMap.get("中西医结合医院")));
        data.add(extractor.apply(hospitalMap.get("民族医医院")));

        series.put("data", data);

        // 如果是0~49范围，添加圆角样式
        if ("0~49".equals(name)) {
            Map<String, Object> itemStyle = new HashMap<>();
            itemStyle.put("barBorderRadius", Arrays.asList(5, 5, 0, 0));
            series.put("itemStyle", itemStyle);
        }

        return series;
    }
}
